package com.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.domain.Org;
import com.service.OrgService;
import com.utils.Ret;

@Controller
public class OrgController {
	
	@Autowired
	OrgService orgService;
	
	@RequestMapping("tochangeOrg.do")
	public String toChangeOrg(Model model){
		List<Org> orgs ;
		try {
			orgs = orgService.getOrgs();
			model.addAttribute("orgs", orgs);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "org/tochangeorg";
	}
	
	@RequestMapping("changeOrg.do")
	public void changeOrg(@RequestParam("ocode")String ocode,
			HttpServletRequest request,PrintWriter out){
		Ret ret = new Ret();
		ret.setStatusCode("200");
		ret.setMessage("机构切换成功！");
		
		try {
			orgService.changeOrg(request,ocode);
			System.out.println(request.getSession().getAttribute("ocode"));
		} catch (Exception e) {
			e.printStackTrace();
			ret.setStatusCode("500");
			ret.setMessage("机构切换失败！");
		}		
		out.print(ret.toJsonString());
		out.flush();
		out.close();
	}
	
}
